import tkinter as tk
from tkinter import ttk
import pandas as pd

class ExcelViewer:
    def __init__(self, root, file_path):
        self.root = root
        self.root.title("Excel Viewer")

        # 读取 Excel 文件
        self.df = pd.read_excel(file_path)  # 读取 Excel 文件

        # 创建表格组件
        self.tree = ttk.Treeview(root)
        self.tree.pack(fill="both", expand=True)

        # 定义表格的列
        self.tree["columns"] = list(self.df.columns)
        self.tree.heading("#0", text="Index")
        for col in self.df.columns:
            self.tree.heading(col, text=col)
            self.tree.column(col, anchor="w", width=100)

        # 将数据插入到表格中
        for index, row in self.df.iterrows():
            values = list(row)
            self.tree.insert("", "end", text=str(index), values=values)

        # 添加滚动条
        self.add_scrollbar()

    def add_scrollbar(self):
        # 创建垂直滚动条
        vsb = ttk.Scrollbar(self.root, orient="vertical", command=self.tree.yview)
        vsb.pack(side="right", fill="y")
        self.tree.configure(yscrollcommand=vsb.set)

        # 创建水平滚动条
        hsb = ttk.Scrollbar(self.root, orient="horizontal", command=self.tree.xview)
        hsb.pack(side="bottom", fill="x")
        self.tree.configure(xscrollcommand=hsb.set)

# 使用示例
if __name__ == "__main__":
    file_path = "C:\\Users\\zackary\\Documents\\文件档案\\Excel\\信息.xlsx"  # 替换为你的 Excel 文件路径
    root = tk.Tk()
    app = ExcelViewer(root, file_path)
    root.mainloop()